/******************************init****************************/
function queryMessage_message(page) {
	var type = $('#message_type').val();
	var parm = null;
	if ('1' == type) {
		parm = $('#sendMessageForm').serialize() + (page != null ? ('&page=' + page) : '');
	} else if ('2' == type) {
		parm = $('#replyMessageForm').serialize() + (page != null ? ('&page=' + page) : '');
	} else if ('3' == type) {
		parm = $('#trashMessageForm').serialize() + (page != null ? ('&page=' + page) : '');
	}
	var url = create_url('message-query');
	sys_loading();
	sys_record_parm('message',parm);
	$.ajax({type:'POST', url:url, data:parm, success: function(result) {
			sys_loading_close();
			if ('1' == type) {
				$('#sendResultList').html(result);
			} else if ('2' == type) {
				$('#replyResultList').html(result);
			} else if ('3' == type) {
				$('#trashResultList').html(result);
			}
		}
	});
}

/********************************list***********************************/
//查看收到邮件详情
function detail_message(code) {
	var parm = {'code':code};
	var url = create_url("message-detail");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'500px', open:true});
}

// 查看发送邮件详情
function detail1_message(code) {
	var parm = {'code':code};
	var url = create_url("message-detail1");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'500px', open:true});
}

function recycleMySendMessage_message(code, title) {
	$.Zebra_Dialog('确定是否删除主题为《'+title + '》 邮件吗？',{
	'type': 'question', 'title': '警告' , 'buttons':[
       {caption:'删除',callback:function() {
    	   var url = create_url('message-recycle');
    	   aidExecuteMessage_message(code, url);
		}},
           {caption:'取消',callback:function(){}
    	}]
	});
}

function markUnread_message(code){
	$.Zebra_Dialog('确认要改变这封邮件的状态吗？',{
		'type': 'question', 'title': '警告' , 'buttons':[
           {caption:'确认',callback:function() {
        	   var url = create_url('message-state');
        	   aidExecuteMessage_message(code, url);
    		}},
           {caption:'取消',callback:function(){}
    	}]
	});
}
function read_message(code){
	$.Zebra_Dialog('确认全部改为已读吗？',{
		'type': 'question', 'title': '警告' , 'buttons':[
           {caption:'确认',callback:function() {
        	   var url = create_url('message-read');
        	   aidExecuteMessage_message(code, url);
    		}},
           {caption:'取消',callback:function(){}
    	}]
	});
}
function aidExecuteMessage_message(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				queryMessage_message();
			} else {
				sys_msg('操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}



function recycleReceiveMessage_message(code, cun, title) {
	$.Zebra_Dialog('确认要删除  ' + cun + ' 发给您，标题为： 《' + title + '》 消息吗？',{
		'type': 'question', 'title': '警告' , 'buttons':[
           {caption:'删除',callback:function() {
        	   var url = create_url('message-delete2');
        	   aidExecuteMessage_message(code, url);
    		}},
           {caption:'取消',callback:function(){}
	    }]
	});
}

function deleteMessage_message(code) {
	$.Zebra_Dialog('确认要彻底删除 这个消息吗？',{
	'type': 'question', 'title': '警告' , 'buttons':[
       {caption:'确认',callback:function() {
			var url = create_url('message-delete');
			var parm = {'code':code};
			sys_submiting();
			$.ajax({type:'POST', url:url, data:parm,
				success: function(result) {
					sys_submiting_close();
					queryMessage_message();
				}
			})}},
       {caption:'取消',callback:function(){}}
		]
	});
}


/*******************edit******/

function sendMessage_message() {
	var parm = $('#editMessageForm').serialize() + '&content=' + fixJqText(Url.encode(mail_content.getData()));//j
	var url = create_url('message-send');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功!');
				sys_load_handle('message');
			}
		}
	});
}

//删除挑出来的一项
function deleteSelectedItem_message(btn) {
	btn.parents('.rep').remove();
}




/**************************************user***********************************/


function aidQueryUser_message(page) {
	var suc = '';
	$('#selectedUserListBody').find('.run').each(function () {
		suc += $(this).val() + ',';
	});
	if (suc.length > 0) {
		suc = suc.substring(0, suc.length - 1);
	}
	console.log(suc);
	var parm = $('#aidQueryUserForm').serialize() + '&suc=' + suc + '&pageSize=10' + (page != null ? ('&page=' + page) : '');
	var url = create_url('message-aidList');

	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#aidQueryUserResultList').html(result);
		}
	});
}

function deleteUser_message(btn) {
	btn.parents('.rep').remove();
	aidQueryUser_message($('#aidQueryUserPageTxt').val());
}


function selectUser_message() {
	var suc = '';
	$('#userListTd').find('.ucs').each(function () {
		suc += $(this).val() + ',';
	});
	if ('' != suc) {
		suc = suc.substring(0, suc.length - 1);
	}
	var parm = {'suc':suc};
	var url = create_url("message-aidInit");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'1000px', height:'700px', open:true});
}

function addUser_message(code, realName, on, email) {
	var tr = $('#tempUserTable').find('.rep').clone(true);
	tr.find('.rn').html(realName).end()
		.find('.on').html(on).end()
		.find('.run').val(code).end()
		.find('.emt').html(email).end()
		.find('.un').val(realName).end()
		.find('.em').val(email).end();
	$('#selectedUserListBody').append(tr);
	aidQueryUser_message($('#aidQueryUserPageTxt').val());
}


function chooseUser_message() {
	var template = $('#tempUserListDiv').find('.rep');
	var userListTd = $('#userListTd');
	userListTd.html('');
	$('#selectedUserListBody').find('.rep').each(function () {
		var pii = template.clone(true);
		pii.find('.title').html($(this).find('.un').val()).end()
			.find('.runs').val($(this).find('.run').val()).end()
			.find('.em').val($(this).find('.em').val()).end();
		userListTd.append(pii);
	});
	$.fn.colorbox.close();
}

function Restoration_message(code) {
   var url = create_url('message-delete1');
   aidExecuteMessage_message(code, url);
}


function addFilesUploadEvent_message() {
	var url = create_url('message-uploads');
	var btn = $('#uploadFilesBtn');
	new AjaxUpload(btn, {
		action:url,
		name:'files',
		multiple: true,
		responseType: 'json',
		onSubmit : function(file, ext) {
			if (ext && /^(zip|rar|txt|doc|docx|xls|xlsx|ppt|pptx|pdf|jpg|png|jpeg|gif|bmp|ZIP|RAR|TXT|DOC|DOCX|XLS|XLSX|PPT|PPTX|PDF|JPG|PNG|JPEG|GIF|BMP)$/.test(ext)){
				this.setData({
					'info':'文件类型正确'
				});
			} else {
				sys_warning('文件类型错误', '只能上传类型为：zip、rar、txt、doc、docx、xls、xlsx、ppt、pptx、pdf、及图片文件!');
				return false;               
			}
			$('#upload_span').html(sys_loading_img);
			this.disable();
		},
		onComplete: function(files, response){
			if ('error' == response.result)
				sys_warning('失败', '上传文件失败，请检查文件名是否正确!');
			else if ('file_type_error' == response.result)
				sys_warning('文件类型错误', '只能上传类型为：JGP、GIF、BMP、ICO、PNG的文件!');
			else {
				var template = $('#tempAnnexListDiv').find('.rep');
				var annexList = $('#annexListTd');
				var index = parseInt($('#mail_an_index').val());
				$.each(response.list, function(i) {
					var pii = template.clone(true);
					pii.find('.title').html(response.list[i].name).end()
						.find('.name').attr('name', 'anList[' + (index) + '].name').val(response.list[i].name).end()//j
						.find('.size').attr('name', 'anList[' + (index) + '].size').val(response.list[i].size).end()
						.find('.uuid').attr('name', 'anList[' + (index) + '].uuid').val(response.list[i].uuid).end()
						.find('.ext').attr('name', 'anList[' + (index) + '].ext').val(response.list[i].ext).end();
					annexList.append(pii);
					index ++;
				});
				this.enable();
				$('#mail_an_index').val(index);
			}
		}
	});
}